package com.scy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.scy.entity.User;
import com.scy.mapper.UserMapper;
import com.scy.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 本类用来演示
 *
 * @author wwh
 * @date 2024/6/26 11:33 $
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override

    public List<User> findAll() {

        List<User> users = userMapper.selectList(null);

        for (User user : users) {
            user.setPd("***********");
        }

        return users;
    }

    @Override
    @Transactional
    public boolean addUser(User user) {

        // 默认密码,pd不为空
        user.setPd("123456");

        //执行mapper中insert函数,返回insert,1成功 0失败
        int insert = userMapper.insert(user);

        return insert>0?true:false;
    }

    @Override
    public boolean delUser(Long id) {

        int i = userMapper.deleteById(id);


        return i>0?true:false;
    }

    @Override
    public boolean updateUser(User user) {

        int i = userMapper.updateById(user);

        return i>0?true:false;
    }

    @Override
    public User findUserById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public IPage<User> getUserPage(Integer pageSize, Integer pageNum) {


        IPage<User> userIPage = new Page<User>(pageNum, pageSize);

        return userMapper.selectPage(userIPage, null);
    }

    @Override
    public User check(User user) {
        return null;
    }

    @Override
    public User findUser(String account, String pwd) {

        LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<>();
        userWrapper
                .eq(User::getName,account)//条件追加username = 获取的account
                .eq(User::getPd,pwd)//and  password=pwd
                .select(User::getName,User::getSex)
        ;//select name,sex

//        select name,sex from user where username=account and password=pwd
        User user = userMapper.selectOne(userWrapper);

        return user;

    }
}
